(function () {
    "use strict";
    var cID,
        MasterCid = $('#MasterCustomerId').text();
    if (window.customerId > -1 && window.customerId != 0) {
        cID = customerId;
    } else if ($('#CustomerId').val() > -1 && $('#CustomerId').val() != "") {
        cID = $('#CustomerId').val();
    } else if (MasterCid > -1 && MasterCid != '') {
        cID = MasterCid;
    } else {
        var removeHash = location.href.substring(0, location.href.indexOf('?'));
        $.ajax({
            type: "GET",
            url: removeHash + ((location.href.indexOf("?") > -1) ? "&" : "?") + "dumpxml=true",
            dataType: "xml",
            async: false,
            success: function (xml) {
                cID = $(xml).find("DataContainer > *:eq(0)").attr("customerId");
            },
            error: function (err) {
              // alert('Error getting DumpXML : ' + err);
              cID = prompt('Enter CID');
            }
        });
    }
    var showAdmin = $('#adminTooolz').is(':visible'),
        showCache = $('#CacheNotification').is(':visible'),
        isStage = document.URL.indexOf('clickmotivefusionstage') > -1;
    if ((!showAdmin) && (!showCache)) {
        var stageLabel = buildStageLabel(),
            domain = (isStage) ? 'clickmotivestage' : 'clickmotive',
            gotURL = getStageProtocol(),
            mobileDisplay = $('#jqt').is(':visible') ? 'text-align:center;display:block;' : 'display:inline-block;',
            mobileWidth = $('#jqt').is(':visible') ? '87%' : '98%',
            notStage = (isStage) ? 'http' : 'https',
            getHtml = '<div id="moveHead" style="height:73px;width:100%;"></div>\
<div id="adminTooolz" style="background:white;border:1px solid black;padding:10px;position:fixed;top:0px;left:0px;z-index:99999;width:' + mobileWidth + ';text-align:left;margin:10px;">\
<span style="color:red !important;">Current page&#39;s CID is' + stageLabel + ' ' + cID + ' -</span>\
<a id="CCfastlane" href="' + notStage + '://fastlane.' + domain + '.com/customer/info/edit/' + cID + '" target="_blank" style="' + mobileDisplay + 'cursor:pointer;padding:4px;margin:5px;color:black !important;border:1px solid;" title="Open Fastlane straight to this customer record.">Open Fastlane</a>\
<a id="CCadmin" href="http://intranet.' + domain + '.com/admin/Customer.aspx?CustomerId=' + cID + '" target="_blank" style="' + mobileDisplay + 'cursor:pointer;padding:4px;margin:5px;color:black !important;border:1px solid;" title="Open admin straight to this customer record.">Open Admin</a>\
<a href="#" id="regCache" style="' + mobileDisplay + 'cursor:pointer;padding:4px;margin:5px;color:black !important;border:1px solid;" title="Clears cache of all domains. Will clear stage cache if on staging domain.">Clear Site Cache</a>\
<a href="#" id="hostCache" style="' + mobileDisplay + 'cursor:pointer;padding:4px;margin:5px;color:black !important;border:1px solid;" title="Clear URLs cache if you switch domains in admin.">Clear URLs Cache</a>\
<a href="#" id="mvcCache" style="' + mobileDisplay + 'cursor:pointer;padding:4px;margin:5px;color:black !important;border:1px solid;" title="Beta setup cache will need to be cleared if you just put a dealer on a Beta Page Product. This is only necessary once after initial setup.">Clear Beta Setup Cache</a>\
<form style="' + mobileDisplay + '" target="_blank" action="https://na11.salesforce.com/search/SearchResults?">\
<input type="text" style="width:200px;" name="str" placeholder="Search Salesforce" title="Opens a new window to SalesForce search. Click ENTER to begin search.">\
</form>\
</div>';
        $("body").prepend(getHtml);
        $('#regCache').click(function (event) {
            event.preventDefault();
            var completeURL = gotURL + 'customer/cache/clear/' + cID;
            executeURL(completeURL);
        });
        $('#hostCache').click(function (event) {
            event.preventDefault();
            var completeURL = gotURL + 'systems/cache/clear/hostname';
            executeURL(completeURL);
        });
        $('#mvcCache').click(function (event) {
            event.preventDefault();
            var completeURL = gotURL + 'systems/cache/clear/mvccustomers';
            executeURL(completeURL);
        });
    } else if (!showCache) {
        $('#adminTooolz').remove();
        $('#moveHead').remove();
    }

    function getStageProtocol() {
        var protocol = (isStage) ? 'http' : 'https',
            domain = (isStage) ? 'clickmotivestage' : 'clickmotive',
            urlDomain = protocol + '://fastlane.' + domain + '.com/';
        return urlDomain;
    }

    function buildStageLabel() {
        var buildstage = (isStage) ? ' <strong>stage</strong>' : '';
        return buildstage;
    }

    function executeURL(completedURL) {
        var stageLabel = buildStageLabel();
        $("body").append('<iframe style="display: none" src="' + completedURL + '"></iframe>');
        $('#adminTooolz').html('<font style="color:black !important;">Server cache cleared for' + stageLabel + ' CID ' + cID + '. Databases will be updated in <span id="countdown">5</span> seconds.</font>').attr('id', 'CacheNotification');
        $('#moveHead').css('height', '73px');
        doCountDown();
    }

    function doCountDown() {
        var countDownElem = $('#countdown');
        var intervalId;
        var countDown = function () {
            var count = countDownElem.text();
            if (count <= 1) {
                $('#CacheNotification').html('Refreshing now.');
                clearInterval(intervalId);
                location.reload();
            } else {
                countDownElem.html(--count);
            }
        };
        intervalId = setInterval(countDown, 1000);
    }
}());